package com.zkt.auth.domain.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.zkt.auth.common.enums.AuthStatusEnum;
import com.zkt.auth.common.constants.AuthConstant;
import com.zkt.auth.domain.convert.AuthPermissionConverter;
import com.zkt.auth.domain.entity.AuthPermissionBO;
import com.zkt.auth.domain.redis.RedisUtil;
import com.zkt.auth.domain.service.AuthPermissionDomainService;
import com.zkt.auth.infra.basic.entity.AuthPermission;
import com.zkt.auth.infra.basic.service.IAuthPermissionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2024/9/30
 * @description 权限领域服务
 **/

@Service
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {
	
	@Resource
	private IAuthPermissionService authPermissionService;
	
	@Resource
	private RedisUtil redisUtil;
	
	/**
	 * 新增权限
	 *
	 * @param authPermissionBO 权限信息
	 * @return 是否成功
	 */
	@Override
	public Boolean add(AuthPermissionBO authPermissionBO) {
		AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertBoToAuthPermission(authPermissionBO);
		
		if (authPermission.getStatus() == null) {
			authPermission.setStatus(AuthStatusEnum.OPEN.getCode());
		}
		if (authPermission.getShow() == null) {
			authPermission.setShow(AuthStatusEnum.OPEN.getCode());
		}
		
		return authPermissionService.save(authPermission);
	}
	
	/**
	 * 修改权限
	 *
	 * @param authPermissionBO 权限信息
	 * @return 是否成功
	 */
	@Override
	public Boolean update(AuthPermissionBO authPermissionBO) {
		AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertBoToAuthPermission(authPermissionBO);
		return authPermissionService.updateById(authPermission);
	}
	
	/**
	 * 删除权限
	 *
	 * @param authPermissionBO 权限信息
	 * @return 是否成功
	 */
	@Override
	public Boolean delete(AuthPermissionBO authPermissionBO) {
		AuthPermission authPermission = AuthPermissionConverter.INSTANCE.convertBoToAuthPermission(authPermissionBO);
		return authPermissionService.removeById(authPermission.getId());
	}
	
	/**
	 * 权限启用/禁用
	 *
	 * @param authPermissionBO 权限信息
	 * @return 是否成功
	 */
	@Override
	public Boolean changeStatus(AuthPermissionBO authPermissionBO) {
		return this.update(authPermissionBO);
	}
	
	/**
	 * 权限展示/隐藏
	 *
	 * @param authPermissionBO 权限信息
	 * @return 是否成功
	 */
	@Override
	public Boolean changeShowStatus(AuthPermissionBO authPermissionBO) {
		return this.update(authPermissionBO);
	}
	
	/**
	 * 查询用户权限
	 *
	 * @param userName 用户名称/账号
	 * @return 权限列表
	 */
	@Override
	public List<String> getPermission(String userName) {
		if (StrUtil.isBlank(userName)) {
			userName = String.valueOf(StpUtil.getLoginId());
		}
		
		String permissionKey = redisUtil.buildKey(AuthConstant.AUTH_PERMISSION_PREFIX, userName);
		List<AuthPermission> permissionList = redisUtil.getList(permissionKey, AuthPermission.class);
		if (CollUtil.isEmpty(permissionList)) {
			return List.of();
		}
		
		return permissionList.stream()
				.map(AuthPermission::getPermissionKey)
				.toList();
	}
}